/**
 * 游戏工具类
 */
package com.key.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import com.key.KeyChineseChessView;

public class Util {
	/**
	 * 可移动点
	 * 
	 * @param x 所在位置横坐标
	 * @param y //所在位置纵坐标
	 * @return
	 */
	public static List<ChessPoint> getPossibleMove(int x, int y) {
		List<ChessPoint> list = new ArrayList<ChessPoint>();
		switch (KeyChineseChessView.position[x][y]) {
		case KeyChineseChessView.R_KING://红
			if (y > 3 && y <= 5 && KeyChineseChessView.position[x][y - 1] <= 7) {
				ChessPoint move = new ChessPoint();
				move.x = x;
				move.y = y - 1;
				list.add(move);
			}
			if (y < 5 && y >= 3 && KeyChineseChessView.position[x][y + 1] <= 7) {
				ChessPoint move = new ChessPoint();
				move.x = x;
				move.y = y + 1;
				list.add(move);
			}
			if (x > 7 && x <= 9 && KeyChineseChessView.position[x - 1][y] <= 7) {
				ChessPoint move = new ChessPoint();
				move.x = x - 1;
				move.y = y;
				list.add(move);
			}
			if (x >= 7 && x < 9 && KeyChineseChessView.position[x + 1][y] <= 7) {
				ChessPoint move = new ChessPoint();
				move.x = x + 1;
				move.y = y;
				list.add(move);
			}
			// Is it see another king
			boolean isSeeBKing = false;
			int cx = x - 1;
			for (cx = x - 1; cx >= 0; cx--) {
				if (KeyChineseChessView.position[cx][y] != KeyChineseChessView.NOCHESS
						&& KeyChineseChessView.position[cx][y] != KeyChineseChessView.B_KING) {
					isSeeBKing = false;
					break;
				} else if (KeyChineseChessView.position[cx][y] == KeyChineseChessView.B_KING) {
					isSeeBKing = true;
					break;
				}
			}
			if (isSeeBKing) {
				ChessPoint move = new ChessPoint();
				move.x = cx;
				move.y = y;
				list.add(move);
			}
			break;
		case KeyChineseChessView.R_BISHOP://红士
			if ((y == 3 || y == 5) && KeyChineseChessView.position[8][4] <= 7) {
				ChessPoint move = new ChessPoint();
				move.x = 8;
				move.y = 4;
				list.add(move);
			} else if (y == 4) {
				if (KeyChineseChessView.position[7][3] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = 7;
					move.y = 3;
					list.add(move);
				}
				if (KeyChineseChessView.position[9][3] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = 9;
					move.y = 3;
					list.add(move);
				}
				if (KeyChineseChessView.position[7][5] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = 7;
					move.y = 5;
					list.add(move);
				}
				if (KeyChineseChessView.position[9][5] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = 9;
					move.y = 5;
					list.add(move);
				}
			}
			break;
		case KeyChineseChessView.R_ELEPHANT://红相
			if (x >= 7
					&& y >= 2
					&& KeyChineseChessView.position[x - 2][y - 2] <= 7
					&& KeyChineseChessView.position[x - 1][y - 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x - 2;
				move.y = y - 2;
				list.add(move);
			}
			if (x <= 7
					&& y >= 2
					&& KeyChineseChessView.position[x + 2][y - 2] <= 7
					&& KeyChineseChessView.position[x + 1][y - 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x + 2;
				move.y = y - 2;
				list.add(move);
			}
			if (x >= 7
					&& y <= 6
					&& KeyChineseChessView.position[x - 2][y + 2] <= 7
					&& KeyChineseChessView.position[x - 1][y + 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x - 2;
				move.y = y + 2;
				list.add(move);
			}
			if (x <= 7
					&& y <= 6
					&& KeyChineseChessView.position[x + 2][y + 2] <= 7
					&& KeyChineseChessView.position[x + 1][y + 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x + 2;
				move.y = y + 2;
				list.add(move);
			}
			break;
		case KeyChineseChessView.R_HORSE://红马
			if (x >= 2
					&& y >= 1
					&& KeyChineseChessView.position[x - 2][y - 1] <= 7
					&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x - 2;
				move.y = y - 1;
				list.add(move);
			}
			if (x >= 1
					&& y >= 2
					&& KeyChineseChessView.position[x - 1][y - 2] <= 7
					&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x - 1;
				move.y = y - 2;
				list.add(move);
			}
			if (x >= 2
					&& y <= 7
					&& KeyChineseChessView.position[x - 2][y + 1] <= 7
					&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x - 2;
				move.y = y + 1;
				list.add(move);
			}
			if (x >= 1
					&& y <= 6
					&& KeyChineseChessView.position[x - 1][y + 2] <= 7
					&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x - 1;
				move.y = y + 2;
				list.add(move);
			}
			if (x <= 7
					&& y <= 7
					&& KeyChineseChessView.position[x + 2][y + 1] <= 7
					&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x + 2;
				move.y = y + 1;
				list.add(move);
			}
			if (x <= 8
					&& y <= 6
					&& KeyChineseChessView.position[x + 1][y + 2] <= 7
					&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x + 1;
				move.y = y + 2;
				list.add(move);
			}
			if (x <= 7
					&& y >= 1
					&& KeyChineseChessView.position[x + 2][y - 1] <= 7
					&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x + 2;
				move.y = y - 1;
				list.add(move);
			}
			if (x <= 8
					&& y >= 2
					&& KeyChineseChessView.position[x + 1][y - 2] <= 7
					&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.NOCHESS) {
				ChessPoint move = new ChessPoint();
				move.x = x + 1;
				move.y = y - 2;
				list.add(move);
			}
			break;
		case KeyChineseChessView.R_CAR://红车
			for (int k = y - 1; k >= 0; k--) {
				if (KeyChineseChessView.position[x][k] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = x;
					move.y = k;
					list.add(move);
					if (KeyChineseChessView.position[x][k] <= 7
							&& KeyChineseChessView.position[x][k] != 0) {
						break;
					}
				} else {
					break;
				}
			}
			for (int k = y + 1; k <= 8; k++) {
				if (KeyChineseChessView.position[x][k] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = x;
					move.y = k;
					list.add(move);
					if (KeyChineseChessView.position[x][k] <= 7
							&& KeyChineseChessView.position[x][k] != 0) {
						break;
					}
				} else {
					break;
				}
			}
			for (int k = x - 1; k >= 0; k--) {
				if (KeyChineseChessView.position[k][y] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = k;
					move.y = y;
					list.add(move);
					if (KeyChineseChessView.position[k][y] <= 7
							&& KeyChineseChessView.position[k][y] != 0) {
						break;
					}
				} else {
					break;
				}
			}
			for (int k = x + 1; k <= 9; k++) {
				if (KeyChineseChessView.position[k][y] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = k;
					move.y = y;
					list.add(move);
					if (KeyChineseChessView.position[k][y] <= 7
							&& KeyChineseChessView.position[k][y] != 0) {
						break;
					}
				} else {
					break;
				}
			}
			break;
		case KeyChineseChessView.R_CANON://红炮
			boolean isStone = false;//是否已有棋子阻隔
			for (int k = y - 1; k >= 0; k--) {
				if (isStone) {
					if (KeyChineseChessView.position[x][k] <= 7
							&& KeyChineseChessView.position[x][k] != 0) {
						ChessPoint move = new ChessPoint();
						move.x = x;
						move.y = k;
						list.add(move);
						break;
					} else if (KeyChineseChessView.position[x][k] > 7) {
						break;
					}
				}

				if (isStone == false
						&& KeyChineseChessView.position[x][k] == KeyChineseChessView.NOCHESS) {
					ChessPoint move = new ChessPoint();
					move.x = x;
					move.y = k;
					list.add(move);
				} else {
					isStone = true;
				}
			}
			isStone = false;
			for (int k = y + 1; k <= 8; k++) {
				if (isStone) {
					if (KeyChineseChessView.position[x][k] <= 7
							&& KeyChineseChessView.position[x][k] != 0) {
						ChessPoint move = new ChessPoint();
						move.x = x;
						move.y = k;
						list.add(move);
						break;
					} else if (KeyChineseChessView.position[x][k] > 7) {
						break;
					}
				}
				if (isStone == false
						&& KeyChineseChessView.position[x][k] == KeyChineseChessView.NOCHESS) {
					ChessPoint move = new ChessPoint();
					move.x = x;
					move.y = k;
					list.add(move);
				} else {
					isStone = true;
				}
			}
			isStone = false;
			for (int k = x - 1; k >= 0; k--) {
				if (isStone) {
					if (KeyChineseChessView.position[k][y] <= 7
							&& KeyChineseChessView.position[k][y] != 0) {
						ChessPoint move = new ChessPoint();
						move.x = k;
						move.y = y;
						list.add(move);
						break;
					} else if (KeyChineseChessView.position[k][y] > 7) {
						break;
					}
				}
				if (isStone == false
						&& KeyChineseChessView.position[k][y] == KeyChineseChessView.NOCHESS) {
					ChessPoint move = new ChessPoint();
					move.x = k;
					move.y = y;
					list.add(move);
				} else {
					isStone = true;
				}
			}
			isStone = false;
			for (int k = x + 1; k <= 9; k++) {
				if (isStone) {
					if (KeyChineseChessView.position[k][y] <= 7
							&& KeyChineseChessView.position[k][y] != 0) {
						ChessPoint move = new ChessPoint();
						move.x = k;
						move.y = y;
						list.add(move);
						break;
					} else if (KeyChineseChessView.position[k][y] > 7) {
						break;
					}
				}
				if (isStone == false
						&& KeyChineseChessView.position[k][y] == KeyChineseChessView.NOCHESS) {
					ChessPoint move = new ChessPoint();
					move.x = k;
					move.y = y;
					list.add(move);
				} else {
					isStone = true;
				}
			}
			break;
		case KeyChineseChessView.R_PAWN://红卒
			if (x <= 4) {
				if (y >= 1 && KeyChineseChessView.position[x][y - 1] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = x;
					move.y = y - 1;
					list.add(move);
				}
				if (y <= 7 && KeyChineseChessView.position[x][y + 1] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = x;
					move.y = y + 1;
					list.add(move);
				}
				if (x >= 1 && KeyChineseChessView.position[x - 1][y] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = x - 1;
					move.y = y;
					list.add(move);
				}
			} else {
				if (KeyChineseChessView.position[x - 1][y] <= 7) {
					ChessPoint move = new ChessPoint();
					move.x = x - 1;
					move.y = y;
					list.add(move);
				}
			}
			break;
		default:
			break;
		}
		return list;
	}

	/**
	 * 移动是否符合规则
	 * 
	 * @param fromX
	 * @param fromY
	 * @param toX
	 * @param toY
	 * @return
	 */
	public static boolean isMoveRule(int fromX, int fromY, int toX, int toY) {
		boolean isRule = false;
		List<ChessPoint> list = getPossibleMove(fromX, fromY);
		for (int i = 0; i < list.size(); i++) {
			ChessPoint cp = list.get(i);
			if (cp.x == toX && cp.y == toY) {
				return true;
			}
		}
		return isRule;
	}

	/**
	 * 计算谁赢了
	 * @return
	 */
	public static int whoIsSuccess() {
		int i, j;
		boolean RedLive = false, BlackLive = false;
		int rAttackCount = 0, bAttackCount = 0;
		for (i = 0; i < 10; i++)
			for (j = 0; j < 9; j++) {
				if (KeyChineseChessView.position[i][j] == KeyChineseChessView.B_KING)
					BlackLive = true;
				if (KeyChineseChessView.position[i][j] == KeyChineseChessView.R_KING)
					RedLive = true;
				//计算各自攻击力
				if (KeyChineseChessView.position[i][j] == KeyChineseChessView.B_HORSE
						|| KeyChineseChessView.position[i][j] == KeyChineseChessView.B_CANON
						|| KeyChineseChessView.position[i][j] == KeyChineseChessView.B_CAR) {
					rAttackCount += 2;
				} else if (KeyChineseChessView.position[i][j] == KeyChineseChessView.B_PAWN) {
					rAttackCount += 1;
				} else if (KeyChineseChessView.position[i][j] == KeyChineseChessView.R_HORSE
						|| KeyChineseChessView.position[i][j] == KeyChineseChessView.R_CANON
						|| KeyChineseChessView.position[i][j] == KeyChineseChessView.R_CAR) {
					bAttackCount += 2;
				} else if (KeyChineseChessView.position[i][j] == KeyChineseChessView.R_PAWN) {
					bAttackCount += 1;
				}
			}
		if (!BlackLive) {
			return 1;
		} else if (!RedLive) {
			return -1;
		}
		//攻击力都为0，和棋
		if (rAttackCount == 0 && bAttackCount == 0) {
			return 99;
		}

		return 0;
	}

	/**
	 * 是否正在将军
	 * 
	 * @return
	 */
	public static boolean IsKillingKing(int x, int y) {
		boolean isKillingKing = false;
		boolean isRed = false;
		if (KeyChineseChessView.position[x][y] > 7) {
			isRed = true;
		}
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 9; j++) {
				x = i;
				y = j;
				if ((isRed && KeyChineseChessView.position[x][y] > 7)
						|| (!isRed && KeyChineseChessView.position[x][y] <= 7 && KeyChineseChessView.position[x][y] > 0)) {
					switch (KeyChineseChessView.position[x][y]) {
					case KeyChineseChessView.R_HORSE://红马的攻击点是否有黑棋将军
						if (x >= 2
								&& y >= 1
								&& KeyChineseChessView.position[x - 2][y - 1] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x >= 1
								&& y >= 2
								&& KeyChineseChessView.position[x - 1][y - 2] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x >= 2
								&& y <= 7
								&& KeyChineseChessView.position[x - 2][y + 1] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x >= 1
								&& y <= 6
								&& KeyChineseChessView.position[x - 1][y + 2] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x <= 7
								&& y <= 7
								&& KeyChineseChessView.position[x + 2][y + 1] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x <= 8
								&& y <= 6
								&& KeyChineseChessView.position[x + 1][y + 2] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.NOCHESS) {
							ChessPoint move = new ChessPoint();
							move.x = x + 1;
							move.y = y + 2;
							isKillingKing = true;
						}
						if (x <= 7
								&& y >= 1
								&& KeyChineseChessView.position[x + 2][y - 1] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.NOCHESS) {
							ChessPoint move = new ChessPoint();
							move.x = x + 2;
							move.y = y - 1;
							isKillingKing = true;
						}
						if (x <= 8
								&& y >= 2
								&& KeyChineseChessView.position[x + 1][y - 2] == KeyChineseChessView.B_KING
								&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						break;
					case KeyChineseChessView.B_HORSE://黑马的攻击点是否有红棋的帅
						if (x >= 2
								&& y >= 1
								&& KeyChineseChessView.position[x - 2][y - 1] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x >= 1
								&& y >= 2
								&& KeyChineseChessView.position[x - 1][y - 2] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x >= 2
								&& y <= 7
								&& KeyChineseChessView.position[x - 2][y + 1] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x >= 1
								&& y <= 6
								&& KeyChineseChessView.position[x - 1][y + 2] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x <= 7
								&& y <= 7
								&& KeyChineseChessView.position[x + 2][y + 1] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						if (x <= 8
								&& y <= 6
								&& KeyChineseChessView.position[x + 1][y + 2] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.NOCHESS) {
							ChessPoint move = new ChessPoint();
							move.x = x + 1;
							move.y = y + 2;
							isKillingKing = true;
						}
						if (x <= 7
								&& y >= 1
								&& KeyChineseChessView.position[x + 2][y - 1] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.NOCHESS) {
							ChessPoint move = new ChessPoint();
							move.x = x + 2;
							move.y = y - 1;
							isKillingKing = true;
						}
						if (x <= 8
								&& y >= 2
								&& KeyChineseChessView.position[x + 1][y - 2] == KeyChineseChessView.R_KING
								&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.NOCHESS) {
							isKillingKing = true;
						}
						break;
					case KeyChineseChessView.R_CAR://红车的攻击点是否有黑棋将军
						for (int k = y - 1; k >= 0; k--) {
							if (KeyChineseChessView.position[x][k] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						for (int k = y + 1; k <= 8; k++) {
							if (KeyChineseChessView.position[x][k] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						for (int k = x - 1; k >= 0; k--) {
							if (KeyChineseChessView.position[k][y] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						for (int k = x + 1; k <= 9; k++) {
							if (KeyChineseChessView.position[k][y] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						break;
					case KeyChineseChessView.B_CAR://黑车的攻击点是否有红棋的帅
						for (int k = y - 1; k >= 0; k--) {
							if (KeyChineseChessView.position[x][k] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						for (int k = y + 1; k <= 8; k++) {
							if (KeyChineseChessView.position[x][k] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						// 绾靛悜鎵弿
						for (int k = x - 1; k >= 0; k--) {
							if (KeyChineseChessView.position[k][y] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						for (int k = x + 1; k <= 9; k++) {
							if (KeyChineseChessView.position[k][y] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
								break;
							} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								break;
							}
						}
						break;
					case KeyChineseChessView.R_CANON://红炮的攻击点是否有黑棋将军
						boolean isStone = false;//是否有棋阻隔
						for (int k = y - 1; k >= 0; k--) {
							if (isStone) {
								if (KeyChineseChessView.position[x][k] == KeyChineseChessView.B_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}

							if (isStone == false
									&& KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						isStone = false;
						for (int k = y + 1; k <= 8; k++) {
							if (isStone) {
								if (KeyChineseChessView.position[x][k] == KeyChineseChessView.B_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}
							if (isStone == false
									&& KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						isStone = false;
						for (int k = x - 1; k >= 0; k--) {
							if (isStone) {
								if (KeyChineseChessView.position[k][y] == KeyChineseChessView.B_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}
							if (isStone == false
									&& KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						isStone = false;
						for (int k = x + 1; k <= 9; k++) {
							if (isStone) {
								if (KeyChineseChessView.position[k][y] == KeyChineseChessView.B_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}
							if (isStone == false
									&& KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						break;
					case KeyChineseChessView.B_CANON://黑炮的攻击点是否有红棋的帅
						isStone = false;
						for (int k = y - 1; k >= 0; k--) {
							if (isStone) {
								if (KeyChineseChessView.position[x][k] == KeyChineseChessView.R_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}

							if (isStone == false
									&& KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						isStone = false;
						for (int k = y + 1; k <= 8; k++) {
							if (isStone) {
								if (KeyChineseChessView.position[x][k] == KeyChineseChessView.R_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}
							if (isStone == false
									&& KeyChineseChessView.position[x][k] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						isStone = false;
						for (int k = x - 1; k >= 0; k--) {
							if (isStone) {
								if (KeyChineseChessView.position[k][y] == KeyChineseChessView.R_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}
							if (isStone == false
									&& KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						isStone = false;
						for (int k = x + 1; k <= 9; k++) {
							if (isStone) {
								if (KeyChineseChessView.position[k][y] == KeyChineseChessView.R_KING) {
									isKillingKing = true;
									break;
								} else if (KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
									break;
								}
							}
							if (isStone == false
									&& KeyChineseChessView.position[k][y] != KeyChineseChessView.NOCHESS) {
								isStone = true;
							}
						}
						break;
					case KeyChineseChessView.R_PAWN://红卒的攻击点是否有黑棋将军
						if (x <= 4) {
							if (y >= 1
									&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
							}
							if (y <= 7
									&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
							}
							if (x >= 1
									&& KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
							}
						} else {
							if (KeyChineseChessView.position[x - 1][y] == KeyChineseChessView.B_KING) {
								isKillingKing = true;
							}
						}
						break;
					case KeyChineseChessView.B_PAWN://黑卒的攻击点是否有红棋的帅
						if (x >= 5) {
							if (y >= 1
									&& KeyChineseChessView.position[x][y - 1] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
							}
							if (y <= 7
									&& KeyChineseChessView.position[x][y + 1] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
							}
							if (x <= 8
									&& KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
							}
						} else {
							if (KeyChineseChessView.position[x + 1][y] == KeyChineseChessView.R_KING) {
								isKillingKing = true;
							}
						}
						break;
					default:
						break;
					}
					if (isKillingKing) {
						return true;
					}
				}
			}
		}

		return isKillingKing;
	}

	/**
	 * toast提醒
	 * @param context
	 * @param strToast 提示字符
	 */
	public static void toastPopWindow(Context context, String strToast) {

		Toast toast = Toast.makeText(context, strToast, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	/**
	 * 写用户信息
	 * @return
	 */
	public static boolean resultWrite() {
		return UserSystemData.UserInfo.write();
	}

	/**
	 * 是否吃了子
	 * @param cm
	 * @return
	 */
	public static boolean isEatChess(ChessMove cm) {
		if (cm.chessTo != KeyChineseChessView.NOCHESS) {
			return true;
		}
		return false;
	}

	/**
	 * 开局库（代码实现）
	 * @param cm
	 * @return
	 */
	public static ChessMove startGameLib(ChessMove cm) {
		if (KeyChineseChessView.moveStack.size() <= 1) {
			//当中炮 马来跳
			if (cm.from.x == 7 && cm.from.y == 1 && cm.to.x == 7
					&& cm.to.y == 4) {
				if (KeyChineseChessView.position[0][1] == KeyChineseChessView.NOCHESS) {
					ChessMove move = new ChessMove();
					move.from.x = 0;
					move.from.y = 7;
					move.to.x = 2;
					move.to.y = 6;
					move.chessFrom = KeyChineseChessView.B_HORSE;
					move.chessTo = KeyChineseChessView.NOCHESS;
					KeyChineseChessView.position[0][7] = KeyChineseChessView.NOCHESS;
					KeyChineseChessView.position[2][6] = KeyChineseChessView.B_HORSE;
					return move;
				} else {
					ChessMove move = new ChessMove();
					move.from.x = 0;
					move.from.y = 1;
					move.to.x = 2;
					move.to.y = 2;
					move.chessFrom = KeyChineseChessView.B_HORSE;
					move.chessTo = KeyChineseChessView.NOCHESS;
					KeyChineseChessView.position[0][1] = KeyChineseChessView.NOCHESS;
					KeyChineseChessView.position[2][2] = KeyChineseChessView.B_HORSE;
					return move;
				}
			}
			if (cm.from.x == 7 && cm.from.y == 7 && cm.to.x == 7
					&& cm.to.y == 4) {
				ChessMove move = new ChessMove();
				move.from.x = 0;
				move.from.y = 7;
				move.to.x = 2;
				move.to.y = 6;
				move.chessFrom = KeyChineseChessView.B_HORSE;
				move.chessTo = KeyChineseChessView.NOCHESS;
				KeyChineseChessView.position[0][7] = KeyChineseChessView.NOCHESS;
				KeyChineseChessView.position[2][6] = KeyChineseChessView.B_HORSE;
				return move;
			}
		}
		return null;
	}
}